home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5113 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  95 lines

  1. Newsgroups: comp.lang.c
  2. Path: usinternet.com!not-for-mail
  3. From: Scott Jibben <sjibben@usinternet.com>
  4. Subject: Re: embedded strtok() calls on different strings?
  5. Message-ID: <311A8D15.6B5A@usinternet.com>
  6. Date: Thu, 08 Feb 1996 17:53:57 -0600
  7. Organization: Jibben Software
  8. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  9. MIME-Version: 1.0
  10. References: <DMGrn7.1Bx0@CompStar.bnr.ca>
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13.  
  14. tzinck@bnr.ca wrote:
  15. > Hello.
  16. > I want to do the following:
  17. > char *string1="This is string one";
  18. > char *string2="This is string two";
  19. > void *vp1;
  20. > void *vp2;
  21. > vp1 = strtok(string1," ");
  22. > while (vp1 != NULL){
  23. >         printf("Tok1 = %s\n", (char *) vp1);
  24. >         vp2 = strtok(string2," ");
  25. >         while (vp2!=NULL){
  26. >                 printf("Tok2 = %s\n", (char *) vp2);
  27. >                 vp2 = strtok(NULL, " ");
  28. >         }
  29. >         vp1 = strtok(NULL, " ");
  30. > }
  31. > But the output is :
  32. > Tok1 = This
  33. > Tok2 = This
  34. > Tok2 = is
  35. > Tok2 = string
  36. > Tok2 = two
  37. > as vp1 gets corrupted. Any thoughts ?
  38.  
  39. strtok() will usually have a static char * in it to do it's work (keep track of where it 
  40. was) when the NULL is passed as the first parm.  I've often wondered how re-entrant 
  41. strtok() for OS/2 or Win32, that is, if two or more threads use strtok() at a time will 
  42. it barf?  I'd guess that they do, but you never know...
  43.  
  44. I've written my own versions of strtok() that are re-entrant (no statics or globals).  
  45. It's pretty simple.  I have the first parm as a char** and I pass back the address of 
  46. the string after the next token in the string using that parm.
  47.  
  48. Although I don't have the code here, here's a quick (may be buggy and probably not 
  49. pretty <g>) example:
  50.  
  51. char * MyStrTok( char **ppsz, char *pszTok )
  52. {
  53.   char * pszRet;
  54.   char * psz;
  55.   if ( NULL == *ppsz )
  56.     return NULL;
  57.   pszRet = *ppsz;
  58.   psz = strstr( *ppsz, pszTok );
  59.   if ( NULL == psz )
  60.   {
  61.     *ppsz = NULL;
  62.   }
  63.   else
  64.   {
  65.     *ppsz = psz + strlen( pszTok );
  66.     *psz = '\x0';
  67.   }
  68.   return pszRet;
  69. }
  70.  
  71. Hope that is enough to get you started.  Step through the above code to verify that it 
  72. works.  I just typed it in here on the fly.
  73.  
  74. -- 
  75. Scott Jibben, Jibben Software
  76. Galactic Overlord & Mines of Gorr BBS Door Games
  77. -----------------------------
  78. [EMAIL] sjibben@usinternet.com
  79. [WWW] http://www.usinternet.com/jsw
  80. [FTP] ftp.europa.com /outgoing/DOORS/jibben
  81. [FTP] ftp.cts.com /pub/dferber/jibben
  82. [FIDO] 1:282/115             [BBS] 612-379-8272 (10 USR 28.8 Couriers)
  83. [VOICE] 612-757-5626         [FAX] 612-757-8687
  84.  
  85.